package cn.tedu.route;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class RouteProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //设置连接信息
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("192.168.144.160");
        cf.setPort(5672);//管理界面15672，rabbitmq客户端连接到rabbitmq服务端，使用5672(默认端口可以省略)
        cf.setUsername("guest");
        cf.setPassword("guest");
        //创建tcp连接对象
        Connection nc = cf.newConnection();
        //通过tcp连接对象创建channel
        Channel cc = nc.createChannel();
        //定义一个交换机，发布订阅模式中，使用的交换机类型是fanout类型的
        cc.exchangeDeclare("route_logs", BuiltinExchangeType.DIRECT);
        //在控制台输入消息进行测试
        while (true){
            Scanner scanner = new Scanner(System.in);
            System.out.print("输入消息:");
            String msg = scanner.nextLine();
            System.out.print("输入路由键:");
            String routingKey = scanner.nextLine();
            //如果msg是quit，跳出循环
            if(msg.equals("quit"))
                break;
            cc.basicPublish("route_logs",routingKey, null,msg.getBytes());
        }
        //关闭连接
        nc.close();
    }
}
